package main

import (
	"fmt"
	"log"

	"mq_demo/internal"

	"github.com/google/uuid"
)

func main() {
	// 设置实例的接入点。
	hostName := "rabbitmq-cn-aic48045w02.cn-hangzhou.amqp-28.net.mq.amqp.aliyuncs.com"
	// 设置实例的静态用户名密码。
	userName := "MjpyYWJiaXRtcS1jbi1haWM0ODA0NXcwMjpMVEFJNXQ3aHdKSjQyTmJQOWJMS0NadVo="
	passWord := "NDRCRjcwREQxODI0MTI2MDI0RjgxNDcxNzg5NzlBQjU5MzU0OEM4QjoxNzQ0ODE0MzY5OTI5"
	// 设置实例的Vhost。
	virtualHost := "test-vhost"

	rabbitmq, err := internal.NewRabbitMQ(&internal.RabbitMQConfig{
		HostName: hostName,
		Username: userName,
		Password: passWord,
		Vhost:    virtualHost,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer rabbitmq.Close()

	// 设置Exchange、Queue和绑定关系。
	exchangeName := "test-exchange"
	queueName := "test-queue"
	bindingKey := "test-routing-key"
	exchangeType := "direct"

	err = rabbitmq.ExchangeDeclare(exchangeName, exchangeType)
	if err != nil {
		log.Fatal(err)
	}
	err = rabbitmq.QueueDeclareAndBind(queueName, bindingKey)
	if err != nil {
		log.Fatal(err)
	}

	// 开始发送消息
	for i := range 10 {
		messageID := uuid.New().String()
		body := fmt.Sprintf("消息发送示例Body-%d", i)
		err = rabbitmq.Publish(messageID, []byte(body))
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("[SendResult] Message sent successfully, messageId: %s, exchange: %s, routingKey: %s", messageID, exchangeName, bindingKey)
	}
}
